// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// This source file is part of the Cangjie project, licensed under Apache-2.0
// with Runtime Library Exception.
//
// See https://cangjie-lang.cn/pages/LICENSE for license information.

#ifndef CANGJIE_MIGRATOR_FILE_IMPORT_INFO
#define CANGJIE_MIGRATOR_FILE_IMPORT_INFO

#include <vector>
#include <memory>

#include "cangjie/AST/Node.h"
#include "Rules/ImportRule.h"

#include "ImportInfo.h"

namespace CangjieMigrator {

class FileImportInfo {
public:
    struct Result {
        //    File: xxxxx.cj
        // 1: package xxxxx
        // 2:
        // 3: import std.aaaaa.*
        // 4: import std.bbbbbb.*
        // 5:
        // 6: main() {
        // 7:     println("Hello World!")
        // 8: }
        // 9:
        // {begin = end = 1} means that will insert before all of that file
        // {begin = end > 1} means that will insert before line begin
        // {begin < end} means thar will replace line begin to (end - 1)
        // {begin > end or begin > 1} is invalid
        int beginLine;
        int endLine;
        std::vector<std::string> lines;

        [[nodiscard]] bool IsValid() const;
        std::string GenerateString() const;
    };

    FileImportInfo();
    FileImportInfo(const FileImportInfo &) = delete;
    FileImportInfo(FileImportInfo &&other) noexcept;
    FileImportInfo &operator=(const FileImportInfo &) = delete;
    FileImportInfo &operator=(FileImportInfo &&) noexcept;

    void Add(const Cangjie::AST::PackageSpec &node, const PositionRange &range);
    void Add(const Cangjie::AST::ImportSpec &node, const PositionRange &range);
    void Add(std::unique_ptr<ImportInfo> info);

    void DirectReplaceChange();
    void Change(const std::set<const Rules::ImportRule *> &matchRules);
    [[nodiscard]] bool NeedImportStdx() const;

    Result Generate() const;

private:
    std::vector<PackageExpression> GetImports() const;

    int beginLine;
    int endLine;
    std::unique_ptr<PositionRange> packageSpecLocation = nullptr;
    std::vector<std::unique_ptr<ImportInfo>> infos;

    bool needImportStdx;
};

} // namespace CangjieMigrator

#endif // CANGJIE_MIGRATOR_FILE_IMPORT_INFO
